I have just started learning programming myself referring "The C Programming Language by Brian Kernighan and Dennis Ritchie". In the following program
Code:
#include <stdio.h>
int main ()
{
int c , n , state , nc , nw , nl ;
state = 0 ;
nc = nw = nl = 0 ;
while ((c = getchar()) != EOF)
{
++nc ;
if (c == '\n')
++nl ;
if (c == ' ' || c == '\n')
state = 0 ;
else if (state == 0 )
{
state = 1 ;
++nw ;
}
printf("%2d%2d%2d%2d\n" , state ,nc , nw , nl ) ;
}
}
i modified a bit by putting printf() in the loop but i cant understand how its working as getchar() reads character by character when it encounters "abc" after reading 'a' the state should be changed to 1 and it would not read b and c .
I may be wrong but that what I understood . Sorry if the question is pretty basic but i don't wanna move forward with broken concepts.
Thanks in advance